//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.web.filter;

import com.farm.authority.FarmAuthorityService;
import com.farm.core.AuthorityService;
import com.farm.core.ParameterService;
import com.farm.core.auth.domain.AuthKey;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.auth.util.Urls;
import com.farm.parameter.FarmParameterService;
import com.farm.web.online.OnlineUserOpImpl;
import com.farm.web.online.OnlineUserOpInter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;

public class FilterValidate implements Filter {
	private static final Logger log = Logger.getLogger(FilterValidate.class);

	public FilterValidate() {
	}

	private boolean isURL(String urlStr) {
		return Urls.isActionByUrl(urlStr, "do") || Urls.isActionByUrl(urlStr, "html");
	}

	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		ParameterService parameterService = FarmParameterService.getInstance();
		String path = ((HttpServletRequest) arg0).getContextPath();
		String basePath = arg0.getScheme() + "://" + arg0.getServerName() + ":" + arg0.getServerPort() + path + "/";
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;
		HttpSession session = request.getSession();
		String requestUrl = request.getRequestURL().toString();
		String formatUrl = Urls.formatUrl(requestUrl,
				requestUrl.indexOf(":") < 8 ? basePath.replace(":80/", "/") : basePath);
		String key = null;
		LoginUser currentUser = null;
		AuthKey authkey = null;
		if (!this.isURL(formatUrl)) {
			arg2.doFilter(arg0, arg1);
		} else {
			request.setAttribute("URL",
					requestUrl + (request.getQueryString() == null ? "" : "?" + request.getQueryString()));
			log.debug(formatUrl + request.getRemoteAddr());
			key = Urls.getActionKey(formatUrl);
			currentUser = (LoginUser) session.getAttribute("USEROBJ");
			AuthorityService authority = FarmAuthorityService.getInstance();
			authkey = authority.getAuthKey(key);
			if (parameterService.getParameter("config.auth.check.url").equals("false")) {
				arg2.doFilter(arg0, arg1);
			} else if (authkey != null && !authkey.isUseAble()) {
				response.sendError(405, "当前用户没有权限请求该资源!");
			} else {
				OnlineUserOpInter ouop = OnlineUserOpImpl.getInstance(request.getRemoteAddr(), session);
				ouop.userVisitHandle();
				String subKey = Urls.getActionSubKey(key);
				if (authkey != null && !authkey.isLogin()) {
					arg2.doFilter(arg0, arg1);
				} else {
					String prefix = parameterService.getParameter("config.url.free.path.prefix");
					if (prefix != null && subKey.indexOf("/" + prefix) == 0) {
						arg2.doFilter(arg0, arg1);
					} else {
						prefix = parameterService.getParameter("config.url.login.path.prefix");
						if (prefix != null && subKey.indexOf("/" + prefix) == 0 && this.isURL(formatUrl)) {
							if (currentUser == null) {
								response.sendRedirect(parameterService.getParameter("config.index.defaultpage"));
							} else {
								arg2.doFilter(arg0, arg1);
							}
						} else if (authkey != null && authkey.isLogin() && !authkey.isCheck()) {
							arg2.doFilter(arg0, arg1);
						} else {
							String[] urls = parameterService.getParameter("config.url.login.indexs").split(",");
							if (this.isURL(formatUrl) && Arrays.asList(urls).contains(key)) {
								arg2.doFilter(arg0, arg1);
							} else if (this.isURL(formatUrl) && currentUser == null) {
								if (request.getMethod().equals("GET")) {
									session.setAttribute(
											parameterService.getParameter("farm.constant.session.key.go.url"),
											requestUrl + "?" + Urls.getUrlParameters(request));
								}

								response.sendRedirect(basePath + parameterService.getParameter("config.url.login"));
							} else {
								Set<String> usraction = (Set) request.getSession().getAttribute("USERACTION");
								if (authkey != null && !usraction.contains(key)) {
									log.info("用户未经授权访问'" + authkey.getTitle() + "(" + key + ")'被拦截");
									response.sendError(405, "当前用户没有权限请求该资源!");
								} else {
									arg2.doFilter(arg0, arg1);
								}
							}
						}
					}
				}
			}
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

	public void destroy() {
	}
}
